﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web.Profile;
using SoftSource.Timecard.Web.DAL;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Web.Providers
{
    public class TimecardProfileProvider : ProfileProvider
    {
        public override string ApplicationName { get; set; }

        public TimecardProfileProvider()
        {
            this.ApplicationName = "TimecardApplication";
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            string value = config["applicationName"];
            if (!string.IsNullOrEmpty(value))
            {
                this.ApplicationName = value;
            }

            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "Timecard Profile provider");
            }

            base.Initialize(name, config);
        }

        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection settingsProperties)
        {
            SettingsPropertyValueCollection settingsValues = new SettingsPropertyValueCollection();
            bool isAuthenticated = Convert.ToBoolean(context["IsAuthenticated"]);
            
            if (!isAuthenticated) 
                return settingsValues;
            
            string userName = context[User.UserNameProperty].ToString();

            User user = Users.GetUserByUserName(userName);
            if (user == null) 
                return settingsValues;

            foreach (SettingsProperty property in settingsProperties)
            {
                SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(property);
                switch (property.Name)
                {
                    case User.UserIDProperty:
                        settingsPropertyValue.PropertyValue = user.UserID;
                        break;
                    case User.FullNameProperty:
                        settingsPropertyValue.PropertyValue = user.FullName;
                        break;
                    default:
                        break;
                }


                settingsValues.Add(settingsPropertyValue);
            }


            return settingsValues;
        }

        #region Not Implemented Methods

        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
        }

        public override int DeleteProfiles(ProfileInfoCollection profiles)
        {
            throw new NotImplementedException();
        }

        public override int DeleteProfiles(string[] usernames)
        {
            throw new NotImplementedException();
        }

        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
